package com.vincent.baseapp.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.vincent.baseapp.R;


/**
 * Created by Administrator on 2015/7/2.
 */
public class DialogUtil {

    private static final String TAG = "DialogUtil";
    private static DialogUtil instance;
    private static Context context;

    public static DialogUtil getInstance(Context mContext) {
        if (instance == null) {
            instance = new DialogUtil();
        }
        context = mContext;
        return instance;
    }

    /**
     * 单按钮通用提示框
     *
     * @param str
     */
    public void SingleAlertDialog(String str) {
        SingleAlertDialog(str, null);
    }

    /**
     * 单按钮通用提示框
     *
     * @param str
     * @param callback
     */
    public void SingleAlertDialog(String str, final SingleDialogInterface callback) {

        final Dialog dialog = new Dialog(context, R.style.My_Dialog_Fullscreen);
        dialog.setContentView(R.layout.dialog_single_click);
        TextView confirmTextView = (TextView) dialog.findViewById(R.id.dialog_button_ok);
        TextView contentTextView = (TextView) dialog.findViewById(R.id.content);
        contentTextView.setText(str);

        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                callback.confirm("");
            }
        });
        dialog.show();
    }


    /**
     * @param str
     * @param callback
     */
    public void DoubleAlertDialog(String str, final DoubleDialogInterface callback) {

        final Dialog dialog = new Dialog(context, R.style.My_Dialog_Fullscreen);
        dialog.setContentView(R.layout.dialog_double_click);
        TextView cancelTextView = (TextView) dialog.findViewById(R.id.dialog_button_cancel);
        TextView confirmTextView = (TextView) dialog.findViewById(R.id.dialog_button_ok);
        TextView contentTextView = (TextView) dialog.findViewById(R.id.content);
        contentTextView.setText(str);
        cancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callback != null) callback.cancel("");
            }
        });
        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callback != null) callback.confirm("");
            }
        });
        dialog.show();
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (2 * display.getWidth() / 3); // 设置宽度
        dialog.getWindow().setAttributes(lp);
    }

    public void showUpdateVersion(String str, int needUpdate, final DoubleDialogInterface
            callback) {

        final Dialog dialog = new Dialog(context, R.style.My_Dialog_Fullscreen);
        dialog.setContentView(R.layout.dialog_update_version);
        TextView cancelTextView = (TextView) dialog.findViewById(R.id.dialog_button_cancel);
        TextView confirmTextView = (TextView) dialog.findViewById(R.id.dialog_button_ok);
        TextView contentTextView = (TextView) dialog.findViewById(R.id.content);
        contentTextView.setText(str);
        if (needUpdate == 1) {
            cancelTextView.setText("退出");
        }
        cancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callback != null) callback.cancel("");
            }
        });
        confirmTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (callback != null) callback.confirm("");
            }
        });
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        dialog.show();
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (2 * display.getWidth() / 3); // 设置宽度
        dialog.getWindow().setAttributes(lp);
    }


    public interface SingleDialogInterface {
        public void confirm(Object object);
    }

    public interface DoubleDialogInterface {
        public void confirm(Object object);

        public void cancel(Object object);
    }

}
